<template>
  <div id="app">
    <a-input :value="inputValue" placeholder="请输入任务" class="my_ipt" @change="changeInputValue" />
    <a-button type="primary" @click="addItemHandle">添加事项</a-button>

    <a-list bordered :dataSource="changeData" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="checkboxChange($event, item.id)">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="deleteItemHandle(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{ finishAll }}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button
            @click="btnChangeStatus('all')"
            :type="btnStatus === 'all' ? 'primary' : 'default'"
          >全部</a-button>
          <a-button
            @click="btnChangeStatus('undone')"
            :type="btnStatus === 'undone' ? 'primary' : 'default'"
          >未完成</a-button>
          <a-button
            @click="btnChangeStatus('done')"
            :type="btnStatus === 'done' ? 'primary' : 'default'"
          >已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clearItem">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations } from 'vuex'
export default {
  name: 'app',
  data() {
    return {}
  },
  computed: {
    ...mapState(['list', 'inputValue', 'btnStatus']),
    ...mapGetters(['finishAll', 'changeData'])
  },
  methods: {
    ...mapMutations([
      'changeBtnStatus',
      'clearFinish',
      'changeStatus',
      'removeItem',
      'addItem',
      'setInputValue'
    ]),
    // 切换完成与否的状态
    btnChangeStatus(status) {
      console.log(status)
      this.changeBtnStatus(status)
    },

    // 清空已经完成的任务
    clearItem() {
      this.clearFinish()
    },

    // 更改 checkbox 状态
    checkboxChange(e, id) {
      const params = {
        done: e.target.checked,
        id
      }
      this.changeStatus(params)
    },

    // 删除事项功能
    deleteItemHandle(id) {
      this.removeItem(id)
    },

    // 触发添加事项功能
    addItemHandle() {
      if (this.inputValue.trim().length === 0) {
        return this.$message.warning('请输入最新事项')
      }
      this.addItem()
    },

    // 获取输入框最新的值
    changeInputValue(e) {
      this.setInputValue(e.target.value)
    }
  },
  created() {
    this.$store.dispatch('getList')
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
